<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="processQualityBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加
                        </button>
                        <button id="processQualityBtnExport" class="layui-btn icon-btn"><i
                                class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="processQualityTable" lay-filter="processQualityTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="processQualityTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="processQualityForm">
    <form lay-filter="processQualityForm" class="layui-form open-form">
        <input name="id" type="hidden"/>
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <label class="layui-form-label">物料类别</label>
                    <input name="productTypeId" type="hidden"/>
                    <div class="layui-input-block" id="selectProductType"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">工序</label>
                    <div class="layui-input-block">
                        <select name="processId" id="processId" lay-verify="required" required>.
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">参数分类</label>
                    <input name="categoryId" type="hidden"/>
                    <div class="layui-input-block" id="selectCategory">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">参数名</label>
                    <div class="layui-input-block">
                        <input name="name" placeholder="请输入参数名" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">单位</label>
                    <div class="layui-input-block">
                        <input name="unitName" placeholder="请输入单位" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">识别码</label>
                    <div class="layui-input-block">
                        <input name="code" placeholder="请输入识别码" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">展示类型</label>
                    <div class="layui-input-block">
                        <select name="type" id="type" lay-filter="type" lay-verify="required" required>
                            <option value="1">单选</option>
                            <option value="2">输入框</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="btnAddDiv">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-inline">
                        <button id="btnAdd" class="layui-btn" lay-filter="btnAdd" onclick="return false;">添加规格值</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否启用</label>
                    <div class="layui-input-block">
                        <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="processQualityFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var treeData = [];
        admin.req('production/qualityTesting/admin/processQualityCategory/getTreeData', {limit: 9999}, function (res) {
            if (res.code == 200) {
                treeData = res.data;
            } else {
                layer.msg('获取质量参数分类失败', {icon: 2, time: 1000});
            }
        }, 'GET', false);
        // 渲染表格
        var insTb = table.render({
            elem: '#processQualityTable',
            url: config.base + 'production/qualityTesting/admin/processQuality/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                , {field: 'productTypeName', align:"center", title: '物料类别'}
                , {field: 'processName', align:"center", title: '工序'}
                , {field: 'categoryName', align:"center", title: '分类'}
                , {field: 'name', align:"center", title: '参数名'}
                , {field: 'code', align:"center", title: '识别码'}
                , {field: 'type', align:"center", title: '展示类型',templet:function (d) {
                            if(d.type==1){
                                return '单选';
                            }else if(d.type==2){
                                return '输入框';
                            }
                    }}
                , {field: 'createTime', align:"center", title: '创建时间'}
                , {align: 'center', toolbar: '#processQualityTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(processQualityTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(processQualityTable)', function (obj) {
            table.reload('processQualityTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#processQualityBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data, formId, parentIdVal) {
            admin.open({
                type: 1,
                area: ['700px', '700px'],
                title: data ? '修改质量参数' : '添加质量参数',
                content: $('#processQualityForm').html(),
                success: function () {

                    //物料类别
                    var xmSelectInstance = null;
                    var useSelect = admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getTreeData', {limit: 99999}, "selectProductType", data ? data.productTypeId : null, "物料类别", "productTypeId", function (row, instance) {
                        //取消选择时 清空物料类别ID
                        xmSelectInstance = instance
                        if (row.arr.length == 0) {
                            $("input[name='productTypeId']").val('');
                        }
                    });

                    var getTemplateHtml=function(i){
                        var labelName='';
                        var valLen=$("form input[name='value']").length;
                        var classNew='';
                        if(valLen==0){
                            labelName='属性值';
                        }else{
                            classNew='newAdd'
                        }
                        var templateHtml=''+
                            '<div class="layui-form-item '+classNew+'" >' +
                            '<div class="layui-form-item">'+
                            '<div >'+
                            '<label class="layui-form-label" >'+labelName+'</label>' +
                            '<div class="layui-input-inline">' +
                            '<input id="value'+i+'" name="processQualityValue" placeholder="请输入属性" type="text" class="layui-input" lay-verify="required" required/>' +
                            '</div>' +
                            '</div>' +
                            '<div >'+
                            // '<div class="layui-input-inline">' +
                            // '<input id="sort'+i+'" name="sort" placeholder="排序值" type="text" class="layui-input" lay-verify="sortNumber" required/>' +
                            // '</div>' +
                            '</div>';

                        templateHtml+='<div class="layui-input-inline">' +
                            '<button id="btnDel'+i+'" class="layui-btn delBtn" lay-filter="btnDel'+i+'" style="margin-left: 20px;">删除</button>' +
                            '</div>';
                        templateHtml+='</div>' + '</div>';

                        return templateHtml;
                    }
                    var i=0;
                    var addTemplateDiv=function () {

                        var html=getTemplateHtml(i++);
                        $("#btnAddDiv").prev().after(html);

                        form.render();
                        // btnCount++;
                        form.render('select');

                        $(".delBtn").off('click').on('click',function(){
                            var btn=$(this);
                            var btnId=btn[0].id;
                            var btnNo=btnId.substring("btnDel".length,btnId.length);

                            btn.parent().parent().parent().remove();

                        });

                    };
                    admin.reqSync('enterprise/product/admin/productType/getAllKnife',{},function (res) {
                        if (res.code == 200) {
                            $.each(res.data, function (index, item) {
                                $("#productTypeId").append(new Option(item.name, item.id));
                            })
                            form.render('select');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }

                    },'GET')


                    $('#btnAdd').click(addTemplateDiv);
                    admin.reqSync('enterprise/product/admin/processInfo/list',{limit:9999},function (res) {
                        if (res.code == 200) {
                            console.log(res.data)
                            $.each(res.data, function (index, item) {
                                $("#processId").append(new Option(item.nodeNames, item.id));
                            })
                            form.render('select');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }

                    }, 'GET')

                    var selectCategory = xmSelect.render({
                        el: '#selectCategory',
                        model: {label: {type: 'text'}},
                        radio: true,
                        clickClose: true,
                        filterable: true,
                        tree: {
                            show: true,
                            strict: false,
                            expandedKeys: [-1],
                        },
                        tips: '顶级分类',
                        height: 'auto',
                        data() {
                            return treeData;
                        },
                        on: function (data) {
                            //被选中的select的id,如果没有 -1
                            let categoryId = data.arr.length > 0 ? data.change[0].value : -1;
                            $("input[name='categoryId']").val(categoryId);
                        },
                    });


                    form.val('processQualityForm', data);

                    form.on('select(type)',function(data){
                        if(data.value==2){
                            $("input[name='processQualityValue']").parent().parent().parent().remove();
                            $("#btnAddDiv").hide();
                        }else {
                            addTemplateDiv();
                            $("#btnAddDiv").show();
                        }
                    })
                    if(data){
                        if(data.type==2){
                            $("input[name='processQualityValue']").parent().parent().parent().remove();
                            $("#btnAddDiv").hide();
                        }else {
                            $("#btnAddDiv").show();
                        }
                        selectCategory.setValue([data.categoryId])
                        if(data.processQualityValue.length>0){
                            console.log(data.processQualityValue)
                            for (var index = 0;i<data.processQualityValue.length;index++){
                                addTemplateDiv();
                                $('#value'+index).val(data.processQualityValue[index])
                            }
                        }
                    }else {
                        addTemplateDiv();
                    }
                    // 表单提交事件
                    form.on('submit(processQualityFormSubmit)', function (d) {
                        var vals = [];
                        $("form input[name='processQualityValue']").each(function (index, item) {
                            var val = $(this).val();
                            vals.push(val);
                        });
                        d.field.processQualityValue = vals;
                        layer.load(2);
                        admin.req(data ? 'production/qualityTesting/admin/processQuality/update' : 'production/qualityTesting/admin/processQuality/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('processQualityTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('production/qualityTesting/admin/processQuality/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#processQualityBtnExport').click(function () {
            var checkRows = table.checkStatus('processQualityTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>